define('DomEvents', [
    'Evento',
], function (Evento) {

    var eventBus = {};
    Evento.convert(eventBus);

    var touchStart = null;
    document.addEventListener('touchstart', function (e) {
        // console.log('touchstart');
        touchStart = [e.clientX, e.clientY];
    }, true);
    document.addEventListener('mousedown', function (e) {
        // console.log('mousedown');
        touchStart = [e.clientX, e.clientY];
        eventBus.emit('documentMousedown', e);
    }, true);
    document.addEventListener('mouseup', function (e) {
        eventBus.emit('documentMouseup', e);
    }, true);
    document.addEventListener('mousemove', function (e) {
        eventBus.emit('documentMousemove', e);
    }, true);
    // document.addEventListener('mouseout', function (e) {
    //     eventBus.emit('documentMouseout', e);
    // }, true);
    // document.addEventListener('mouseover', function (e) {
    //     eventBus.emit('documentMouseover', e);
    // }, true);

    var hypot = Math.hypot || function (x, y, z) {
        return Math.sqrt(x * x + y * y + z * z);
    };

    document.addEventListener('click', function (e) {

        //> 10px, skip
        if (touchStart && hypot(e.clientX - touchStart[0], e.clientY - touchStart[1]) > 10) {
            // console.log('>10px, skip')
            return;
        }

        eventBus.emit('documentClick', e);
    }, true);

    document.addEventListener('dblclick', function (e) {

        //> 10px, skip
        if (touchStart && hypot(e.clientX - touchStart[0], e.clientY - touchStart[1]) > 10) {
            // console.log('>10px, skip')
            return;
        }

        eventBus.emit('documentDblClick', e);
    }, true);

    var bindResize = null;

    (function () {
        var throttle = function (type, name, obj, bus) {
            obj = obj || window;
            bus = bus || eventBus;
            var running = false;
            var func = function () {
                if (running) {
                    return;
                }
                running = true;
                requestAnimationFrame(function () {
                    var e = new CustomEvent(name);
                    obj.dispatchEvent(e);
                    running = false;
                    bus.emit('optimizedResize', e);
                });
            };
            obj.addEventListener(type, func);
        };

        bindResize = function (obj, bus) {
            throttle("resize", "optimizedResize", obj, bus);
        };

        /* init - you can init any event */
        throttle("resize", "optimizedResize");
    })();

    return {
        eventBus: eventBus,
        bindResize: bindResize
    };

});